package com.pengliu.controller;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pengliu.common.bean.SysUser;
import com.pengliu.config.BaseController;
import com.pengliu.service.SysUserService;
import com.pengliu.util.AjaxResult;
import com.pengliu.util.Constants;

@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController{
	
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * user: pengliu
	 * date: 2017年9月30日
	 * desc: 编辑资料页面
	 * @return
	 */
	@RequestMapping("/toDatumEditPage")
	public ModelAndView toDatumEditPage() {
		ModelAndView modelAndView = this.getModelAndView("/sys_user/datum_edit");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/updateDatum")
	public AjaxResult updateDatum(
		@RequestParam String realName,
		@RequestParam String password
	) {
		SysUser sysUser = getCurrentSysUser();
		SysUser newUser = new SysUser();
		BeanUtils.copyProperties(sysUser, newUser);
		newUser.setRealName(realName);
		newUser.setPassword(new StandardPasswordEncoder(Constants.PWDKEY).encode(password));
		int count = sysUserService.updateByPrimaryKeySelective(newUser);
		return AjaxResult.getSuccessrResult(count>0);
	}
}
